﻿@model DLULearningNetwork.ViewModels.PagerViewModel2
@{
    if (Model.Url != null)
    {
        if (Model.Url.LastIndexOf('?') == -1)
        {
            Model.Url = Model.Url + "?page=";
        }
        else
        {
            int index = Model.Url.LastIndexOf("page=");
            if (index == -1)
            {
                Model.Url = Model.Url + "&page=";
            }
            else
            {
                string newUrl = Model.Url.Substring(index, Model.Url.Length - index);
                string[] lst = newUrl.Split('&');
                Model.Url = Model.Url.Replace(lst[0], "") + "&page=";
                Model.Url = Model.Url.Replace("&&", "&").Replace("?&", "?");
            }
        }
    }

    string url = "";
    //Upto 2 pages
    if (Model.TotalPages > 1)
    {
        // Show First Prev Page
        if (Model.CurrentPage > 1)
        {
            if (Model.Url == null)
            {
    <li><a href="#1" page="1">@Model.lblFirst</a></li>
    <li><a href="#@(Model.CurrentPage - 1)" page="@(Model.CurrentPage - 1)">@Model.lblPrev</a></li>
            }
            else
            {
                url = Model.Url + "1";
    <li><a href="@url" page="1">@Model.lblFirst</a></li>    
                           url = Model.Url + (Model.CurrentPage - 1).ToString();
    <li><a href="@url" page="@(Model.CurrentPage - 1)">@Model.lblPrev</a></li>
            }
        }

        /*--Show page numbers--*/

        int start = Model.CurrentPage - (int)(Model.PageSize / 2);
        int end = Model.CurrentPage + (int)((Model.PageSize / 2) - 1);
        if (start < 1)
        {
            start = 1; end = (Model.PageSize > Model.TotalPages) ? Model.TotalPages : Model.PageSize;
        }
        if (end > Model.TotalPages)
        {
            end = Model.TotalPages; start = ((Model.TotalPages - Model.PageSize + 1) > 1) ? (Model.TotalPages - Model.PageSize + 1) : 1;
        }

        for (int i = start; i <= end; i++)
        {
            if (i == Model.CurrentPage)
            {
    <li class="current">@i</li>
            }
            else
            {
                if (Model.Url == null)
                {
    <li><a href="#@i" page="@i">@i</a></li>    
                }
                else
                {
                    url = Model.Url + i.ToString();
    <li><a href="@url" page="@i">@i</a></li>
                }
            }
        }

        /*---Show Next Last Page--*/
        if (Model.CurrentPage < Model.TotalPages)
        {
            if (Model.Url == null)
            {
    <li><a href="#@(Model.CurrentPage + 1)" page="@(Model.CurrentPage + 1)">@Model.lblNext</a></li>
    <li><a href="#@Model.TotalPages" page="@Model.TotalPages">@Model.lblLast</a></li>                            
            }
            else
            {
                url = Model.Url + (Model.CurrentPage + 1).ToString();
    <li><a href="@url" page="@(Model.CurrentPage + 1)">@Model.lblNext</a></li>
                                                  url = Model.Url + (Model.TotalPages).ToString();
    <li><a href="@url" page="@Model.TotalPages">@Model.lblLast</a></li>                                        
            }
        }
    }
}
